function getQueryVariable(variable) {
    var query = window.location.search.substring(1);
    var vars = query.split("&");
    for (var i = 0; i < vars.length; i++) {
        var pair = vars[i].split("=");
        if (pair[0] == variable) {
            return pair[1];
        }
    }
    return (false);
}

$(() => {
    let title = decodeURI(getQueryVariable("title"));
    let description = decodeURI(getQueryVariable("description"));
    if (title !== 'false') {
        let bloglist = document.querySelector('#right > div.mainContent > ul > li:nth-child(2) > div');
        let newBlog = '<div class="blog-item">\n' +
            '                                <div class="blog-title">\n' +
            '                                    <h2 style="display: inline;">\n' +
            '                                        <!--博文图标-->\n' +
            '                                        <span class="blog-icon">\n' +
            '                                            <i class="layui-icon layui-icon-form"></i>\n' +
            '                                        </span>\n' +
            '                                        <span class="blog_title">' + title + '</span></h2>\n' +
            '                                    <!--删除按钮-->\n' +
            '                                    <span class="deleteIcon" onclick="delBlog(this)">\n' +
            '                                        <i class="layui-icon layui-icon-delete"></i> 删除\n' +
            '                                    </span>\n' +
            '                                    <!--编辑按钮-->\n' +
            '                                    <span class="editIcon" onclick="editBlog(this)">\n' +
            '                                        <i class="layui-icon layui-icon-edit"></i> 编辑\n' +
            '                                    </span>\n' +
            '\n' +
            '                                </div>\n' +
            '                                <div class="blog-abstract" style="padding: 15px">\n' +
            '                                    <div style="    font-size: 0.9rem;\n' +
            '    line-height: 1.5;">2018-05-22 12:19:03\n' +
            '                                    </div>\n' +
            '                                    <div style="    font-size: 1.1rem;\n' +
            '                                    border-left: 5px solid #409eff;\n' +
            '    line-height: 1.5;\n' +
            '    color: rgb(48, 49, 51);\n' +
            '    padding: 0px 10px 0px 15px;" class="blog_description">' + description + '\n' +
            '                                    </div>\n' +
            '                                </div>\n' +
            '                                <div class="blog-content">\n' +
            '                                    <span class="blog_content">1.在任意软件复制一个成语，本软件将会自动查找成语信息，并且将该成语的\n' +
            '随机“接龙成语”复制到剪切板，长按任意输入框即可粘贴。\n' +
            '（QQ接龙红包神器 φ(^∇^ ) ）\n' +
            '　　2. 点击搜索图标，在输入框内输入成语，点击输入法回车键，\n' +
            '软件将会自动查找成语信息;所有内容可复制.\n' +
            '　　3. 点击小飞机图标，软件会自动查询接龙成语的成语信息。\n' +
            '\n' +
            '　　※ 1. 接龙成语指符合成语接龙规则的后一个成语.\n' +
            '　　※ 2. 移协成语数据库目前仅收录30804条成语。\n' +
            '　　※ 3. 移协图标可点击.\n' +
            '　　※ 4. 意见反馈可提交至：邮箱：kazin7853@gmail.com</span>\n' +
            '                                </div>\n' +
            '                                <p class="labels">\n' +
            '                                    <span>\n' +
            '                                        <i class="layui-icon layui-icon-note"></i> Android\n' +
            '                                    </span>\n' +
            '                                    <span>\n' +
            '                                    <i class="layui-icon layui-icon-note"></i> Java\n' +
            '                                </span>\n' +
            '                                    <span class="read" style="margin:0 50px">\n' +
            '                                        <a href="blogDetail.html">阅读全文\n' +
            '                                            <i class="layui-icon layui-icon-next"></i></a></span>\n' +
            '                                </p>\n' +
            '                            </div>';
        $(bloglist).append(newBlog);
    }
});
/*选项卡*/
$('#right > div.navBar > ul > li').click(function () {
    $(this).addClass('active').siblings().removeClass('active');
    $('.mainContent ul li').eq($(this).index()).show().siblings().hide()
});

function editBlog(blog_button) {
    if (localStorage.isLogin === "true") {
        let title = blog_button.parentNode.querySelector('h2 > span.blog_title').innerText.trim();
        let description = blog_button.parentNode.parentNode.querySelector('div.blog-abstract > div.blog_description').innerHTML.trim();
        let content = blog_button.parentNode.parentNode.querySelector('div.blog-content > span').innerHTML.trim();
        window.location.href = "./EditBlog.html?title=" + title + "&description=" + description + '&content=' + content;
    } else {
        layer.confirm('您尚未登录，现在登录吗？', {icon: 2, title: '提示'}, function (index) {
            window.location.href = "./login.html";
            layer.close(index);
        });
    }
}

function delBlog(blog_button) {
    if (localStorage.isLogin === "true") {
        blog_button.parentNode.parentNode.parentNode.removeChild(blog_button.parentNode.parentNode);
    } else {
        layer.confirm('您尚未登录，现在登录吗？', {icon: 2, title: '提示'}, function (index) {
            window.location.href = "./login.html";
            layer.close(index);
        });
    }
}

let logout = () => {
    localStorage.clear();
    window.location.href = './login.html'
};

function searchBlog() {
    let span = document.querySelector('#right > div.mainContent > ul > li:nth-child(2) > div > div.searchBar > div > div > span')
    let keyword = $(span).siblings('input').val();
    let blog_list = document.querySelector('#right > div.mainContent > ul > li:nth-child(2) > div');
    let allblogItems = blog_list.querySelectorAll('.blog-item');
    console.log(allblogItems);
    allblogItems.forEach((e) => {
        /*console.log(e,"none?");
        console.log(e.querySelector('h2 > span.blog_title').innerHTML);*/
        let title = e.querySelector('h2 > span.blog_title').innerHTML;
        let isInclude = title.indexOf(keyword);
        if (isInclude === -1)
            $(e).hide();
        else {
            $(e).show();
        }
    });
}

function searchProj() {
    let span = document.querySelector('#right > div.mainContent > ul > li:nth-child(3) > div > div.searchBar > div > div > span')
    let keyword = $(span).siblings('input').val();
    let proj_list = document.querySelector('#right > div.mainContent > ul > li:nth-child(3) > div');
    let allblogItems = proj_list.querySelectorAll('div.proj-item');
    console.log(allblogItems);
    allblogItems.forEach((e) => {
        /*console.log(e,"none?");
        console.log(e.querySelector('h2 > span.blog_title').innerHTML);*/
        let title = e.querySelector('h2 > span.proj_title').innerHTML;
        let isInclude = title.indexOf(keyword);
        if (isInclude === -1)
            $(e).hide();
        else {
            $(e).show();
        }
    });
}
